package com.jcli.jb51_net.article._绘制神经元素;

import javax.swing.*;
import java.awt.*;  
import java.awt.event.MouseAdapter;  
import java.awt.event.MouseEvent;  
  
public class NeuralNetworkGUI extends JFrame {  
    private static final int NODE_RADIUS = 20;  
    private static final int NODE_COUNT = 10;  
    private static final int NODE_SPACING = 50;  
    private static final int DETAIL_WIDTH = 200;  
    private static final int DETAIL_HEIGHT = 100;  
  
    private JPanel mainPanel;  
    private JDialog detailDialog;  
  
    public NeuralNetworkGUI() {  
        setTitle("Neural Network GUI");  
        setSize(600, 400);  
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  
        mainPanel = new JPanel() {  
            @Override  
            protected void paintComponent(Graphics g) {  
                super.paintComponent(g);  
                drawNodes(g);  
            }  
        };  
        mainPanel.setLayout(null);  
        mainPanel.setPreferredSize(new Dimension(600, 400));  
        add(mainPanel);  
  
        detailDialog = new JDialog(this, "Node Details", true);  
        detailDialog.setSize(DETAIL_WIDTH, DETAIL_HEIGHT);  
        detailDialog.setLayout(new BorderLayout());  
        detailDialog.setLocationRelativeTo(this);  
        detailDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);  
  
        addNodeMouseListener();  
    }  
  
    private void addNodeMouseListener() {  
        mainPanel.addMouseListener(new MouseAdapter() {  
            @Override  
            public void mouseClicked(MouseEvent e) {  
                int x = e.getX();  
                int y = e.getY();  
                for (int i = 0; i < NODE_COUNT; i++) {  
                    int nodeX = NODE_SPACING * i + NODE_RADIUS;  
                    int nodeY = NODE_RADIUS;  
                    if (isInsideNode(x, y, nodeX, nodeY)) {  
                        showDetails(i);  
                        break;  
                    }  
                }  
            }  
        });  
    }  
  
    private boolean isInsideNode(int x, int y, int nodeX, int nodeY) {  
        int distanceX = x - nodeX;  
        int distanceY = y - nodeY;  
        return distanceX * distanceX + distanceY * distanceY <= NODE_RADIUS * NODE_RADIUS;  
    }  
  
    private void drawNodes(Graphics g) {  
        Graphics2D g2d = (Graphics2D) g;  
        g2d.setColor(Color.BLUE);  
        for (int i = 0; i < NODE_COUNT; i++) {  
            int x = NODE_SPACING * i + NODE_RADIUS;  
            int y = NODE_RADIUS;  
            g2d.fillOval(x, y, NODE_RADIUS * 2, NODE_RADIUS * 2);  
        }  
    }  
  
    private void showDetails(int nodeIndex) {  
        JLabel detailLabel = new JLabel("Details for Node " + (nodeIndex + 1));  
        detailDialog.add(detailLabel, BorderLayout.CENTER);  
        detailDialog.setVisible(true);  
    }  
  
    public static void main(String[] args) {  
        SwingUtilities.invokeLater(() -> {  
            NeuralNetworkGUI gui = new NeuralNetworkGUI();  
            gui.setVisible(true);  
        });  
    }  
}